const Router = require('@koa/router');
const wechatController = require('../controllers/wechat.controller');
const { checkAuth, checkPermission } = require('../middlewares/auth');

const router = new Router({
  prefix: '/api/v1/wechat'
});

/**
 * @swagger
 * /api/v1/wechat/login:
 *   post:
 *     tags: [微信服务]
 *     summary: 微信登录
 *     description: 使用微信小程序 code 进行登录认证
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - code
 *               - appId
 *             properties:
 *               code:
 *                 type: string
 *                 description: 微信小程序登录凭证code
 *               appId:
 *                 type: string
 *                 description: 微信小程序的AppID
 *     responses:
 *       200:
 *         description: 登录成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     token:
 *                       type: string
 *                       description: JWT认证令牌
 *                     user:
 *                       type: object
 *                       properties:
 *                         id:
 *                           type: string
 *                         openid:
 *                           type: string
 *                         unionid:
 *                           type: string
 *       400:
 *         description: 参数错误或code无效
 *
 */
router.post('/login', wechatController.login);

/**
 * @swagger
 * /api/v1/wechat/payment/create:
 *   post:
 *     tags: [微信服务]
 *     summary: 创建微信支付订单
 *     description: 创建微信支付订单（支持小程序支付和H5支付）
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - appId
 *               - orderNo
 *               - totalFee
 *               - description
 *               - paymentType
 *             properties:
 *               appId:
 *                 type: string
 *                 description: 微信小程序/公众号的AppID
 *               orderNo:
 *                 type: string
 *                 description: 商户订单号
 *               totalFee:
 *                 type: integer
 *                 description: 支付金额（单位：分）
 *               description:
 *                 type: string
 *                 description: 商品描述
 *               paymentType:
 *                 type: string
 *                 enum: [JSAPI, H5]
 *                 description: 支付类型（JSAPI为小程序支付，H5为网页支付）
 *               openid:
 *                 type: string
 *                 description: 用户openid（JSAPI支付必填）
 *     responses:
 *       200:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     prepayId:
 *                       type: string
 *                       description: 预支付交易会话标识
 *                     payParams:
 *                       type: object
 *                       description: 支付参数
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/payment/create', checkAuth, wechatController.createPayment);

/**
 * @swagger
 * /api/v1/wechat/payment/query/{orderNo}:
 *   get:
 *     tags: [微信服务]
 *     summary: 查询支付订单
 *     description: 查询微信支付订单状态
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: orderNo
 *         required: true
 *         schema:
 *           type: string
 *         description: 商户订单号
 *     responses:
 *       200:
 *         description: 查询成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     tradeState:
 *                       type: string
 *                       description: 交易状态
 *                     tradeStateDesc:
 *                       type: string
 *                       description: 交易状态描述
 *                     transactionId:
 *                       type: string
 *                       description: 微信支付订单号
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 订单不存在
 */
router.get('/payment/query/:orderNo', checkAuth, wechatController.queryPayment);

/**
 * @swagger
 * /api/v1/wechat/payment/refund:
 *   post:
 *     tags: [微信服务]
 *     summary: 申请退款
 *     description: 对已支付的订单申请退款
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - orderNo
 *               - refundNo
 *               - totalFee
 *               - refundFee
 *             properties:
 *               orderNo:
 *                 type: string
 *                 description: 原商户订单号
 *               refundNo:
 *                 type: string
 *                 description: 退款单号
 *               totalFee:
 *                 type: integer
 *                 description: 原订单金额（单位：分）
 *               refundFee:
 *                 type: integer
 *                 description: 退款金额（单位：分）
 *               reason:
 *                 type: string
 *                 description: 退款原因
 *     responses:
 *       200:
 *         description: 退款申请成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     refundId:
 *                       type: string
 *                       description: 微信退款单号
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 订单不存在
 */
router.post('/payment/refund', checkAuth, wechatController.refundPayment);

/**
 * @swagger
 * /api/v1/wechat/apps:
 *   post:
 *     tags: [微信服务]
 *     summary: 创建小程序
 *     description: 创建新的微信小程序配置
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - appId
 *               - appSecret
 *               - name
 *             properties:
 *               appId:
 *                 type: string
 *                 description: 微信小程序的AppID
 *               appSecret:
 *                 type: string
 *                 description: 微信小程序的AppSecret
 *               name:
 *                 type: string
 *                 description: 小程序名称
 *               description:
 *                 type: string
 *                 description: 小程序描述
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                     name:
 *                       type: string
 *                     appId:
 *                       type: string
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/apps', checkAuth, checkPermission('wechat:write'), wechatController.createApp);

/**
 * @swagger
 * /api/v1/wechat/apps:
 *   get:
 *     tags: [微信服务]
 *     summary: 获取所有小程序
 *     description: 获取所有已配置的微信小程序列表
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                       name:
 *                         type: string
 *                       appId:
 *                         type: string
 *                       description:
 *                         type: string
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/apps', checkAuth, checkPermission('wechat:read'), wechatController.getAllApps);

/**
 * @swagger
 * /api/v1/wechat/apps/{id}:
 *   get:
 *     tags: [微信服务]
 *     summary: 获取单个小程序
 *     description: 获取指定ID的微信小程序配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 小程序ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                     name:
 *                       type: string
 *                     appId:
 *                       type: string
 *                     description:
 *                       type: string
 *       404:
 *         description: 小程序不存在
 */
router.get('/apps/:id', checkAuth, checkPermission('wechat:read'), wechatController.getApp);

/**
 * @swagger
 * /api/v1/wechat/apps/{id}:
 *   put:
 *     tags: [微信服务]
 *     summary: 更新小程序
 *     description: 更新指定ID的微信小程序配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 小程序ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               appSecret:
 *                 type: string
 *                 description: 微信小程序的AppSecret
 *               name:
 *                 type: string
 *                 description: 小程序名称
 *               description:
 *                 type: string
 *                 description: 小程序描述
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                     name:
 *                       type: string
 *                     appId:
 *                       type: string
 *       404:
 *         description: 小程序不存在
 */
router.put('/apps/:id', checkAuth, checkPermission('wechat:write'), wechatController.updateApp);

/**
 * @swagger
 * /api/v1/wechat/apps/{id}:
 *   delete:
 *     tags: [微信服务]
 *     summary: 删除小程序
 *     description: 删除指定ID的微信小程序配置
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 小程序ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *       404:
 *         description: 小程序不存在
 */
router.delete('/apps/:id', checkAuth, checkPermission('wechat:write'), wechatController.deleteApp);

/**
 * @swagger
 * /api/v1/wechat/qrcode:
 *   post:
 *     tags: [微信服务]
 *     summary: 生成小程序码
 *     description: 生成微信小程序的二维码或小程序码
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - appId
 *               - path
 *             properties:
 *               appId:
 *                 type: string
 *                 description: 小程序AppID
 *               path:
 *                 type: string
 *                 description: 小程序页面路径
 *               width:
 *                 type: integer
 *                 description: 二维码宽度（像素）
 *                 default: 430
 *     responses:
 *       200:
 *         description: 生成成功
 *         content:
 *           image/png:
 *             schema:
 *               type: string
 *               format: binary
 *       400:
 *         description: 参数错误
 *       404:
 *         description: 小程序不存在
 */
router.post('/qrcode', checkAuth, checkPermission('wechat:write'), wechatController.generateQRCode);

/**
 * @swagger
 * /api/v1/wechat/message/template:
 *   post:
 *     tags: [微信服务]
 *     summary: 发送订阅消息
 *     description: 发送微信小程序订阅消息
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - appId
 *               - touser
 *               - templateId
 *               - data
 *             properties:
 *               appId:
 *                 type: string
 *                 description: 小程序AppID
 *               touser:
 *                 type: string
 *                 description: 接收者的openid
 *               templateId:
 *                 type: string
 *                 description: 订阅消息模板ID
 *               data:
 *                 type: object
 *                 description: 模板数据
 *               page:
 *                 type: string
 *                 description: 点击消息跳转的页面
 *     responses:
 *       200:
 *         description: 发送成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *       400:
 *         description: 参数错误
 *       404:
 *         description: 小程序不存在
 */
router.post('/message/template', checkAuth, checkPermission('wechat:write'), wechatController.sendTemplateMessage);

/**
 * @swagger
 * /api/v1/wechat/notify/{appId}:
 *   post:
 *     tags: [微信服务]
 *     summary: 处理支付回调
 *     description: 处理微信支付的异步通知
 *     parameters:
 *       - in: path
 *         name: appId
 *         required: true
 *         schema:
 *           type: string
 *         description: 微信应用ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/xml:
 *           schema:
 *             type: object
 *             description: 微信支付通知数据（XML格式）
 *     responses:
 *       200:
 *         description: 处理成功
 *         content:
 *           application/xml:
 *             schema:
 *               type: object
 *               properties:
 *                 return_code:
 *                   type: string
 *                   example: "SUCCESS"
 *                 return_msg:
 *                   type: string
 *                   example: "OK"
 *       400:
 *         description: 通知数据验证失败
 *       404:
 *         description: 应用不存在
 */
router.post('/notify/:appId', wechatController.handlePaymentNotify);

module.exports = router; 